package com.tuanzi.loan.web.bean.statistics;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.service.statistics.SmsStatisticsService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.statistics.SmsStatisticsRequest;
import com.tuanzi.loan.vo.response.statistics.SmsStatisticsResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.statistics.SmsStatisticsLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import java.io.IOException;
import java.util.List;
import java.util.Optional;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SmsStatisticsBean extends JSFListingBean<SmsStatisticsRequest, SmsStatisticsResponse> {

	private static final long serialVersionUID = 8608081435004046254L;

	@Autowired
	private SmsStatisticsService smsStatisticsService;

	private static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance("yyyy-MM-dd");

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	private void initLazyDataModel() {
		$ = new SmsStatisticsRequest();
		lazyDataModel = new SmsStatisticsLazyDataModel($);
	}

	public void onReset() {
		initLazyDataModel();
	}

	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(DictionaryCode.SMS_PLATFORM_CHANNEL);
	}


	public void exportExcel() throws IOException {
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("导出结果");
		HSSFCellStyle cellStyle = wb.createCellStyle();
		cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
		cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);

		// 为导出全部数据同时兼容原来的分页查询接口
		PageSearcher<SmsStatisticsRequest> srcSearcher = ((SmsStatisticsLazyDataModel) lazyDataModel).getPageSearcher();
		PageSearcher<SmsStatisticsRequest> destSearcher = new PageSearcher<>(srcSearcher.getSearchCondition(), new PageRequest(0, Integer.MAX_VALUE));
		Page<SmsStatisticsResponse> list = smsStatisticsService.list(destSearcher);

		int nextResponseIndex = 0;

		HSSFRow responseHeaderRow = sheet.createRow(nextResponseIndex);
		nextResponseIndex++;

		HSSFCell responseHeaderCell = responseHeaderRow.createCell(0);
		responseHeaderCell.setCellValue("执行时间");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(1);
		responseHeaderCell.setCellValue("短链接");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(2);
		responseHeaderCell.setCellValue("发送数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(3);
		responseHeaderCell.setCellValue("点击数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(4);
		responseHeaderCell.setCellValue("H5登记数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(5);
		responseHeaderCell.setCellValue("H5注册数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(6);
		responseHeaderCell.setCellValue("登录数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(7);
		responseHeaderCell.setCellValue("注册数");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(8);
		responseHeaderCell.setCellValue("发送任务ID");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(9);
		responseHeaderCell.setCellValue("名称");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(10);
		responseHeaderCell.setCellValue("发送文案");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(11);
		responseHeaderCell.setCellValue("发送通道");
		responseHeaderCell.setCellStyle(cellStyle);
		responseHeaderCell = responseHeaderRow.createCell(12);
		responseHeaderCell.setCellValue("短信数量");
		responseHeaderCell.setCellStyle(cellStyle);

		for (SmsStatisticsResponse response : list) {
			HSSFRow responseValueRow = sheet.createRow(nextResponseIndex);
			nextResponseIndex++;

			HSSFCell responseValueCell = responseValueRow.createCell(0);
			responseValueCell.setCellValue(response.getExecuteTime() == null ? null : DATE_FORMAT.format(response.getExecuteTime()));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(1);
			responseValueCell.setCellValue(Optional.ofNullable(response.getShortLink()).orElse(null));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(2);
			responseValueCell.setCellValue(Optional.ofNullable(response.getSendCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(3);
			responseValueCell.setCellValue(Optional.ofNullable(response.getClickCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(4);
			responseValueCell.setCellValue(Optional.ofNullable(response.getH5RegistrationCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(5);
			responseValueCell.setCellValue(Optional.ofNullable(response.getH5RegistrationCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(6);
			responseValueCell.setCellValue(Optional.ofNullable(response.getLoginCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);
			responseValueCell = responseValueRow.createCell(7);
			responseValueCell.setCellValue(Optional.ofNullable(response.getRegisterCount()).orElse(0));
			responseValueCell.setCellStyle(cellStyle);

			for (SmsStatisticsResponse.SendSmsTask task : response.getSendTasks()) {
				responseValueCell = responseValueRow.createCell(8);
				responseValueCell.setCellValue(Optional.ofNullable(task.getId()).orElse(null));
				responseValueCell.setCellStyle(cellStyle);
				responseValueCell = responseValueRow.createCell(9);
				responseValueCell.setCellValue(Optional.ofNullable(task.getName()).orElse(null));
				responseValueCell.setCellStyle(cellStyle);
				responseValueCell = responseValueRow.createCell(10);
				responseValueCell.setCellValue(Optional.ofNullable(task.getContent()).orElse(null));
				responseValueCell.setCellStyle(cellStyle);
				responseValueCell = responseValueRow.createCell(11);
				responseValueCell.setCellValue(Optional.ofNullable(translate(DictionaryCode.SMS_PLATFORM_CHANNEL,task.getPlatformChannel())).orElse(null));
				responseValueCell.setCellStyle(cellStyle);
				responseValueCell = responseValueRow.createCell(12);
				responseValueCell.setCellValue(Optional.ofNullable(task.getSendCount()).orElse(0));
				responseValueCell.setCellStyle(cellStyle);

				responseValueRow = sheet.createRow(nextResponseIndex);
				nextResponseIndex++;
			}
		}

		sheet.autoSizeColumn(0, true);
		sheet.autoSizeColumn(1, true);
		sheet.autoSizeColumn(2, true);
		sheet.autoSizeColumn(3, true);
		sheet.autoSizeColumn(4, true);
		sheet.autoSizeColumn(5, true);
		sheet.autoSizeColumn(6, true);
		sheet.autoSizeColumn(7, true);
		sheet.autoSizeColumn(8, true);
		sheet.autoSizeColumn(9, true);
		sheet.autoSizeColumn(10, true);
		sheet.autoSizeColumn(11, true);
		sheet.autoSizeColumn(12, true);

		FacesContext facesContext = FacesContext.getCurrentInstance();
		ExternalContext externalContext = facesContext.getExternalContext();
		externalContext.setResponseContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		externalContext.setResponseHeader("Content-Disposition", "attachment; filename=export.xls");
		wb.write(externalContext.getResponseOutputStream());
		facesContext.responseComplete();
	}
}
